
// 验证字符串中是否存在汉字
function isChina(val) {
  let reg = new RegExp('[\\u4E00-\\u9FFF]+','g')
  if (reg.test(val)) {
    // 含有中文字符
    return true
  } else {
    return false
  }
}

// 判断是否是图片
function  isImage(param) {
  let isArr = ['png', 'jpg', 'jpeg', 'pcx', 'psd', 'tiff', 'bmp', 'webp']
  function sliceStr(str) {
    const arr = str.split('.')
    const strs = arr[arr.length - 1]
    return isArr.includes(strs.toLocaleLowerCase())
  }
  if (Array.isArray(param)) {
    for (let item of param) {
      if (!sliceStr(item)) {
        return false
      } else {
        continue
      }
    }
    return true
  } else {
    return sliceStr(param)
  }
}



//判断输入的是不是手机号

function isPhone(phone){
  if (!phone || phone.length < 11) return false
  var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
  if (!myreg.test(phone)) {
    return false;
  } else {
    return true;
  }
}

function isName(name) {
  if (!name) return false
  let reg = /[0-9]/
  if (reg.test(name)) {
    return false
  } else {
    return true
  }
}



//截取字符串最后一位
function sliceLastStr(str){

      var len = str.length
      str = str.slice(0,len-1)

      return str

}

//截取字符串
function sliceStr(str,num=1){

      var len = str.length - 5
      str = str.slice(len-num,len)

      return str

}


// 格式化时间
function  formatData(date) {
  if (!date) return false
  date = new Date(date);
  let Y = date.getFullYear()
  let M = date.getMonth() + 1
  let D = date.getDate()
  let h = date.getHours()
  let m = date.getMinutes()
  let s = date.getSeconds()
  function Time(time) {
    return time > 9 ? time : '0' + time
  }
  return `${Y}-${Time(M)}-${Time(D)} ${Time(h)}:${Time(m)}:${Time(s)}`
}

// 获取当前时间
function  formatNowDate(type=1) {
  let date = new Date();
  let Y = date.getFullYear()
  let M = date.getMonth() + 1
  let D = date.getDate()
  let h = date.getHours()
  let m = date.getMinutes()
  let s = date.getSeconds()
  function Time(time) {
    return time > 9 ? time : '0' + time
  }
  if(type==1){
	  return `${Y}-${Time(M)}-${Time(D)} ${Time(h)}:${Time(m)}:${Time(s)}`
  }else{
	  return `${Y}-${Time(M)}-${Time(D)}`
  }
}


//数组数据处理
function oldArray(val){
	return JSON.parse(JSON.stringify(val))
}

//获取序号
function returnNumber(page,index,pageSize=10){
   var page = page*1 - 1
   var index = index++
   var str
   if(index==pageSize){
      str = (page+1)*pageSize
   }else{
      str = page*pageSize + index
   }
   return str+1
}
//判断是不是JSON
function isJSON_test(str) {
        if (typeof str == 'string') {
            try {
                var obj=JSON.parse(str);
                return true;
            } catch(e) {
                return false;
            }
        }else{
			return false;
		}
        
 }
 
 function hasClass(ele, cls) {
   return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'))
 }
 /**
  * Add class to element
  * @param {HTMLElement} elm
  * @param {string} cls
  */
  function addClass(ele, cls) {
   if (!hasClass(ele, cls)) ele.className += ' ' + cls
 }
 
 /**
  * Remove class from element
  * @param {HTMLElement} elm
  * @param {string} cls
  */
  function removeClass(ele, cls) {
   if (hasClass(ele, cls)) {
     const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
     ele.className = ele.className.replace(reg, ' ')
   }
 }
 
 function defaultImg(header_img){
 	if(!header_img){
 	    header_img = 'https://apishopsys.xucheng.top/uplode/timg.jpg'
		return header_img
 	}
 	// return 'https://sinacloud.net/header-img/' + header_img
	return '/imageHeader/url/' + header_img
 }　
 
 function generateRandomString(length) {
   let str = new Date().getTime() + Math.random().toString(36).substring(2, length + 2);
   return str;
 }
 
 function getRamNumber(len){
	 var result="";
	 for(var i=0;i<len;i++){
		 result+=Math.floor(Math.random()*9);
	 }
	 return result
 }
 
 //数组去重
 function unique(arr,key){
	 for(let i=0;i<arr.length;i++){
		 for(let j=i+1;j<arr.length;j++){
			 if(arr[i][key]==arr[j][key]){
				 arr.splice(j,1)
				 j--
			 }
		 }
	 }
	 return arr
 }
 
 // 数组合并
 function mergeDuplicates(arr,ttype){
	 var result = []; // 存放结果的新数组
	 var res = []
		
	 for (var i = 0; i < arr.length; i++) {
		 if(arr[i].code){
			 let index
			 if(ttype==1){
				  index = result.findIndex((item)=>{
				 	return arr[i].price == item.price &&  arr[i].goods_name == item.goods_name && arr[i].inst_store_number == item.inst_store_number && arr[i].remark == item.remark
				 })
			 }else{
				 index = result.findIndex((item)=>{
				 	return arr[i].sales_price == item.sales_price &&  arr[i].goods_name == item.goods_name && arr[i].out_order_number == item.out_order_number && arr[i].remark == item.remark
				 })
			 }
			 
			 if(index<0){
				  result.push(arr[i]);
			 }else{
				  res.push(arr[i]);
			 }
		 }else{
			  result.push(arr[i]);
		 }
		 
		 
		 // if (!result.includes(arr[i].order_number)) { // 如果当前元素不在结果数组中
			//  result.push(arr[i]); // 则添加到结果数组中
		 // }else{
			//  res.push(arr[i])
		 // }
	 }
	
	 for(let item of result){
		 let goods_number = 1
		 let pay_ids = [item.id]
		 for(let ele of res){
			 if(ttype==1){
				 if(ele.price==item.price &&  ele.goods_name==item.goods_name && ele.inst_store_number == item.inst_store_number && ele.remark == item.remark){
					 goods_number++
					 pay_ids.push(ele.id)
				 }
			 }else{
				if(ele.sales_price==item.sales_price &&  ele.goods_name==item.goods_name && ele.out_order_number == item.out_order_number && ele.remark == item.remark){
					 goods_number++
					 pay_ids.push(ele.id)
				}
			 }
			 
		 }
		 item.pay_ids = JSON.stringify(pay_ids)
		 item.goods_number = goods_number
	 }
	 
	 return result;
 }
 
  function exeCommandCopyText(text) {
         try {
             const t = document.createElement('textarea')
             t.nodeValue = text
             t.value = text
             document.body.appendChild(t)
             t.select()
             document.execCommand('copy');
             document.body.removeChild(t)
             return true
         } catch (e) {
             console.log(e)
             return false
         }
     }
 
 // 小数如果是.00转换成整数
 function toggleInt(number){
 	number = number+''
 	let numberArr = number.split('.')
 	if(numberArr[1] == '00'){
 		number = parseInt(number)
 	}
 	return number
 }
 
 // 过滤空字符串
 
 function filternusp(val)
 {
	 return val.replace(/ /g,'')
 }

 // 生成订单编号
 function createOrderSn(length=4) {
	let str = new Date().getTime() + Math.random().toString(9).substring(2, length + 2);
   // let str = new Date().getTime() + Math.random().toString(36).substring(2, length + 2);
   return revlesOrderSn(str);
 }
 
 // 订单编号转换为年月日时分秒
 function revlesOrderSn(str) {
	 let date = str.substring(0,13)*1
	     date = new Date(date)
	 let Y = date.getFullYear()
	 let M = date.getMonth() + 1
	 let D = date.getDate()
	 let h = date.getHours()
	 let m = date.getMinutes()
	 let s = date.getSeconds()
	 function Time(time) {
	   return time > 9 ? time : '0' + time
	 }
	 let lastStr = str.substr(-4)
	 return `${Y}${Time(M)}${Time(D)}${Time(h)}${Time(m)}${Time(s)}${lastStr}`
 }
 
export default {
	createOrderSn,
	revlesOrderSn,
	filternusp,
	toggleInt,
  exeCommandCopyText,
  mergeDuplicates,
  sliceStr,	
  unique,
  getRamNumber,
  formatNowDate,
  generateRandomString,	
  defaultImg,	
  isJSON_test,
  returnNumber,
  oldArray,
  isChina,
  isImage,
  isPhone,
  isName,
  sliceLastStr,
  formatData,
  addClass,
  removeClass
}
